{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 音乐网站用户流失预测 -- Songs和Members特征工程\n",
    "\n",
    "### 数据集说明\n",
    "\n",
    "项目提供KKBOX用户——歌曲重复播放记录，以及用户和歌曲的元数据。训练数据由2017年2月服务到期的用户构成，target标签代表用户在2017年3月是否续订了业务。测试集中的数据由2017年3月内将到期的用户构成，需要预测用户是否在到期后的一个月内即2017年4月预定、流失的概率。\n",
    "\n",
    "以下是文件及字段说明：\n",
    "\n",
    "1. train.csv: 训练数据，共7,377,418条记录 \n",
    "\n",
    "    msno: 用户id，加密String\n",
    "\n",
    "    song_id: song id，歌曲id\n",
    "\n",
    "    source_system_tab: 触发事件的类型/tab，用于表示app的功能类型\n",
    "\n",
    "    source_screen_name: 用户看到的布局的名字（name of the layout）\n",
    "\n",
    "    source_type: 用户在app上播放音乐的入口的类型\n",
    "\n",
    "    target: 标签。1表示用户在第一次听音乐后会在一个月内继续订阅，0表示没有订阅。\n",
    "\n",
    "2. test.csv ：测试数据，共2,556,790条记录 \n",
    "\n",
    "    id: id (用于结果提交)\n",
    "\n",
    "    msno: 用户id\n",
    "\n",
    "    song_id: 歌曲id\n",
    "\n",
    "    source_system_tab: 触发事件的类型/tab，用于表示app的功能类型\n",
    "\n",
    "    source_screen_name: 用户看到的布局的名字（name of the layout）\n",
    "\n",
    "    source_type: 用户在app上播放音乐的入口的类型\n",
    "\n",
    "3. sampleSubmission.csv：提交结果文件样例 \n",
    "\n",
    "    提交测试结果包含两个字段，分别为测试样本id及其标签为1的概率，格式如下：\n",
    "\n",
    "    id,target\n",
    "    \n",
    "    2,0.3\n",
    "    \n",
    "    5,0.1\n",
    "    \n",
    "    6,1\n",
    "    \n",
    "    etc.\n",
    "\n",
    "4. songs.csv：歌曲元数据信息，用unicode编码 \n",
    "\n",
    "    song_id：歌曲id\n",
    "\n",
    "    song_length: 单位为ms\n",
    "\n",
    "    genre_ids: genre 类别. 可多选，用 “|“隔开\n",
    "\n",
    "    artist_name：歌手\n",
    "\n",
    "    composer：作曲\n",
    "\n",
    "    lyricist：作词\n",
    "\n",
    "    language：语言\n",
    "\n",
    "5. members.csv：用户元数据信息\n",
    "\n",
    "    msno：用户id\n",
    "\n",
    "    city：城市\n",
    "\n",
    "    bd: 年龄。注意：年龄数据有离群点\n",
    "\n",
    "    gender：性别\n",
    "\n",
    "    registered_via: 注册方式\n",
    "\n",
    "    registration_init_time: 注册时间，格式为%Y%m%d\n",
    "\n",
    "    expiration_date: 到期时间，格式为 %Y%m%d\n",
    "\n",
    "6. song_extra_infos.csv：歌曲额外的信息\n",
    "\n",
    "    song_id：歌曲id\n",
    "\n",
    "    song name ：歌曲名字\n",
    "\n",
    "    isrc – 国际标准音像制品编码(International Standard Recording Code )。理论上可用于歌曲id，但产生的ISR没有经过官方授权。因此ISRC中的信息，如国家代码和参考年份可能不正确。且多首歌曲可能共享共一个ISRC，因为一首歌曲的音像制可发行多次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 导入工具包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "# 要注意的是一旦导入了seaborn，matplotlib的默认作图风格就会被覆盖成seaborn的格式\n",
    "import seaborn as sns\n",
    "\n",
    "# 矩阵完整显示\n",
    "np.set_printoptions(threshold=np.inf)\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、songs.csv特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>song_id</th>\n",
       "      <th>song_length</th>\n",
       "      <th>genre_ids</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>composer</th>\n",
       "      <th>lyricist</th>\n",
       "      <th>language</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CXoTN1eb7AI+DntdU1vbcwGRV4SCIDxZu+YD8JP8r4E=</td>\n",
       "      <td>247640</td>\n",
       "      <td>465</td>\n",
       "      <td>張信哲 (Jeff Chang)</td>\n",
       "      <td>董貞</td>\n",
       "      <td>何啟弘</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>o0kFgae9QtnYgRkVPqLJwa05zIhRlUjfF7O1tDw0ZDU=</td>\n",
       "      <td>197328</td>\n",
       "      <td>444</td>\n",
       "      <td>BLACKPINK</td>\n",
       "      <td>TEDDY|  FUTURE BOUNCE|  Bekuh BOOM</td>\n",
       "      <td>TEDDY</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DwVvVurfpuz+XPuFvucclVQEyPqcpUkHR0ne1RQzPs0=</td>\n",
       "      <td>231781</td>\n",
       "      <td>465</td>\n",
       "      <td>SUPER JUNIOR</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>dKMBWoZyScdxSkihKG+Vf47nc18N9q4m58+b4e7dSSE=</td>\n",
       "      <td>273554</td>\n",
       "      <td>465</td>\n",
       "      <td>S.H.E</td>\n",
       "      <td>湯小康</td>\n",
       "      <td>徐世珍</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>W3bqWd3T+VeHFzHAUfARgW9AvVRaF4N5Yzm4Mr6Eo/o=</td>\n",
       "      <td>140329</td>\n",
       "      <td>726</td>\n",
       "      <td>貴族精選</td>\n",
       "      <td>Traditional</td>\n",
       "      <td>Traditional</td>\n",
       "      <td>52.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        song_id  song_length genre_ids  \\\n",
       "0  CXoTN1eb7AI+DntdU1vbcwGRV4SCIDxZu+YD8JP8r4E=       247640       465   \n",
       "1  o0kFgae9QtnYgRkVPqLJwa05zIhRlUjfF7O1tDw0ZDU=       197328       444   \n",
       "2  DwVvVurfpuz+XPuFvucclVQEyPqcpUkHR0ne1RQzPs0=       231781       465   \n",
       "3  dKMBWoZyScdxSkihKG+Vf47nc18N9q4m58+b4e7dSSE=       273554       465   \n",
       "4  W3bqWd3T+VeHFzHAUfARgW9AvVRaF4N5Yzm4Mr6Eo/o=       140329       726   \n",
       "\n",
       "        artist_name                            composer     lyricist  language  \n",
       "0  張信哲 (Jeff Chang)                                  董貞          何啟弘       3.0  \n",
       "1         BLACKPINK  TEDDY|  FUTURE BOUNCE|  Bekuh BOOM        TEDDY      31.0  \n",
       "2      SUPER JUNIOR                                 NaN          NaN      31.0  \n",
       "3             S.H.E                                 湯小康          徐世珍       3.0  \n",
       "4              貴族精選                         Traditional  Traditional      52.0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(2296833, 7)"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dpath = '../data/'\n",
    "\n",
    "df_songs_new_dataset = pd.read_csv(dpath + 'songs.csv')\n",
    "df_songs_new_dataset.head()\n",
    "df_songs_new_dataset.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 2296833 entries, 0 to 2296832\n",
      "Data columns (total 7 columns):\n",
      "song_id        object\n",
      "song_length    int64\n",
      "genre_ids      object\n",
      "artist_name    object\n",
      "composer       object\n",
      "lyricist       object\n",
      "language       float64\n",
      "dtypes: float64(1), int64(1), object(5)\n",
      "memory usage: 122.7+ MB\n"
     ]
    }
   ],
   "source": [
    "df_songs_new_dataset.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1、查看每个特征的缺失值数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "song_id              0\n",
       "song_length          0\n",
       "genre_ids        94145\n",
       "artist_name          0\n",
       "composer       1071607\n",
       "lyricist       1945727\n",
       "language             0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_songs_new_dataset.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **由于作曲(composer)和作词(lyricist)缺失值太多了，而且参考意义不大，所以直接删除。歌曲长度(song_length)也没有参考意义，也删除**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>song_id</th>\n",
       "      <th>genre_ids</th>\n",
       "      <th>artist_name</th>\n",
       "      <th>language</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CXoTN1eb7AI+DntdU1vbcwGRV4SCIDxZu+YD8JP8r4E=</td>\n",
       "      <td>465</td>\n",
       "      <td>張信哲 (Jeff Chang)</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>o0kFgae9QtnYgRkVPqLJwa05zIhRlUjfF7O1tDw0ZDU=</td>\n",
       "      <td>444</td>\n",
       "      <td>BLACKPINK</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DwVvVurfpuz+XPuFvucclVQEyPqcpUkHR0ne1RQzPs0=</td>\n",
       "      <td>465</td>\n",
       "      <td>SUPER JUNIOR</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>dKMBWoZyScdxSkihKG+Vf47nc18N9q4m58+b4e7dSSE=</td>\n",
       "      <td>465</td>\n",
       "      <td>S.H.E</td>\n",
       "      <td>3.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>W3bqWd3T+VeHFzHAUfARgW9AvVRaF4N5Yzm4Mr6Eo/o=</td>\n",
       "      <td>726</td>\n",
       "      <td>貴族精選</td>\n",
       "      <td>52.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        song_id genre_ids       artist_name  \\\n",
       "0  CXoTN1eb7AI+DntdU1vbcwGRV4SCIDxZu+YD8JP8r4E=       465  張信哲 (Jeff Chang)   \n",
       "1  o0kFgae9QtnYgRkVPqLJwa05zIhRlUjfF7O1tDw0ZDU=       444         BLACKPINK   \n",
       "2  DwVvVurfpuz+XPuFvucclVQEyPqcpUkHR0ne1RQzPs0=       465      SUPER JUNIOR   \n",
       "3  dKMBWoZyScdxSkihKG+Vf47nc18N9q4m58+b4e7dSSE=       465             S.H.E   \n",
       "4  W3bqWd3T+VeHFzHAUfARgW9AvVRaF4N5Yzm4Mr6Eo/o=       726              貴族精選   \n",
       "\n",
       "   language  \n",
       "0       3.0  \n",
       "1      31.0  \n",
       "2      31.0  \n",
       "3       3.0  \n",
       "4      52.0  "
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_songs_new_dataset = df_songs_new_dataset.drop(['song_length', 'composer', 'lyricist'], axis=1)\n",
    "df_songs_new_dataset.head()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2、分别对每个特征预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （1）genre_ids特征处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "465                568019\n",
       "958                176397\n",
       "2022               168908\n",
       "1609               166492\n",
       "2122               139972\n",
       "1259               101511\n",
       "921                 67586\n",
       "1152                48744\n",
       "359                 43607\n",
       "786                 43005\n",
       "726                 34976\n",
       "139                 34884\n",
       "1011                34024\n",
       "940                 33498\n",
       "1572|275            24346\n",
       "1955                20982\n",
       "691                 19481\n",
       "139|125|109         17615\n",
       "873                 17548\n",
       "437                 17213\n",
       "947                 17108\n",
       "388                 16780\n",
       "458                 15439\n",
       "444                 14861\n",
       "1616                14191\n",
       "242                 13761\n",
       "451                 13284\n",
       "880                 13060\n",
       "423                 11819\n",
       "829                 11762\n",
       "                    ...  \n",
       "409|2189|798            1\n",
       "423|139                 1\n",
       "2163                    1\n",
       "465|822                 1\n",
       "465|1259|139            1\n",
       "2122|242                1\n",
       "388|1138                1\n",
       "1152|139                1\n",
       "1180|437                1\n",
       "1969|1011|2100          1\n",
       "1040|423                1\n",
       "242|691                 1\n",
       "1969|940|2100           1\n",
       "409|2122                1\n",
       "465|1011|691            1\n",
       "1633|958                1\n",
       "691|423                 1\n",
       "921|465|109             1\n",
       "829|1103                1\n",
       "444|388                 1\n",
       "2130|1259               1\n",
       "465|139|109|958         1\n",
       "388|430                 1\n",
       "109|94                  1\n",
       "409|1609                1\n",
       "242|1259                1\n",
       "786|2086                1\n",
       "1609|2079               1\n",
       "1609|726                1\n",
       "1208|786                1\n",
       "Name: genre_ids, Length: 1046, dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "genre_ids = df_songs_new_dataset['genre_ids'].value_counts() \n",
    "genre_ids"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **genre_ids是音乐流派分类，由于缺失值也比较多，而且特征比较重要，所以用CountVectorizer转为稀疏矩阵，对缺失值不做处理**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0                      465\n",
      "1                      444\n",
      "2                      465\n",
      "3                      465\n",
      "4                      726\n",
      "5          864 857 850 843\n",
      "6                      458\n",
      "7                      465\n",
      "8                      465\n",
      "9                 352 1995\n",
      "10                    2157\n",
      "11                     465\n",
      "12                     726\n",
      "13                     458\n",
      "14                     359\n",
      "15                     359\n",
      "16                     458\n",
      "17                     465\n",
      "18                     726\n",
      "19                     465\n",
      "20                     465\n",
      "21                     465\n",
      "22                     465\n",
      "23                     465\n",
      "24                    1609\n",
      "25                    1609\n",
      "26                     465\n",
      "27                     139\n",
      "28                    1609\n",
      "29                     465\n",
      "                ...       \n",
      "2296803               1259\n",
      "2296804                880\n",
      "2296805                465\n",
      "2296806                958\n",
      "2296807                465\n",
      "2296808        139 125 109\n",
      "2296809                465\n",
      "2296810               1259\n",
      "2296811                359\n",
      "2296812               2022\n",
      "2296813                958\n",
      "2296814                388\n",
      "2296815               1011\n",
      "2296816               1616\n",
      "2296817                958\n",
      "2296818                465\n",
      "2296819               2122\n",
      "2296820                359\n",
      "2296821                465\n",
      "2296822          1616 2058\n",
      "2296823                958\n",
      "2296824                465\n",
      "2296825           1572 275\n",
      "2296826                465\n",
      "2296827               2122\n",
      "2296828                958\n",
      "2296829                465\n",
      "2296830               1609\n",
      "2296831                465\n",
      "2296832                829\n",
      "Name: genre_ids, Length: 2296833, dtype: object\n"
     ]
    }
   ],
   "source": [
    "# 先把|符号替换成空格\n",
    "genre_ids_rep = df_songs_new_dataset['genre_ids'].map(lambda x : str(x).replace('|', ' '))\n",
    "print(genre_ids_rep)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "vectorizer = CountVectorizer()\n",
    "\n",
    "# test_lit = ['465','444','864|857|850|843','2122']\n",
    "# print(vectorizer.fit_transform(test_lit).toarray())\n",
    "\n",
    "genre_ids_V = vectorizer.fit_transform(genre_ids_rep)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2296833, 192)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "genre_ids_V.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "column_name = []\n",
    "for i in range (genre_ids_V.shape[1]):\n",
    "    column_name.append('genre_ids_matrix_' + str(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>genre_ids_matrix_0</th>\n",
       "      <th>genre_ids_matrix_1</th>\n",
       "      <th>genre_ids_matrix_2</th>\n",
       "      <th>genre_ids_matrix_3</th>\n",
       "      <th>genre_ids_matrix_4</th>\n",
       "      <th>genre_ids_matrix_5</th>\n",
       "      <th>genre_ids_matrix_6</th>\n",
       "      <th>genre_ids_matrix_7</th>\n",
       "      <th>genre_ids_matrix_8</th>\n",
       "      <th>genre_ids_matrix_9</th>\n",
       "      <th>...</th>\n",
       "      <th>genre_ids_matrix_182</th>\n",
       "      <th>genre_ids_matrix_183</th>\n",
       "      <th>genre_ids_matrix_184</th>\n",
       "      <th>genre_ids_matrix_185</th>\n",
       "      <th>genre_ids_matrix_186</th>\n",
       "      <th>genre_ids_matrix_187</th>\n",
       "      <th>genre_ids_matrix_188</th>\n",
       "      <th>genre_ids_matrix_189</th>\n",
       "      <th>genre_ids_matrix_190</th>\n",
       "      <th>genre_ids_matrix_191</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>10 rows × 192 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   genre_ids_matrix_0  genre_ids_matrix_1  genre_ids_matrix_2  \\\n",
       "0                   0                   0                   0   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "5                   0                   0                   0   \n",
       "6                   0                   0                   0   \n",
       "7                   0                   0                   0   \n",
       "8                   0                   0                   0   \n",
       "9                   0                   0                   0   \n",
       "\n",
       "   genre_ids_matrix_3  genre_ids_matrix_4  genre_ids_matrix_5  \\\n",
       "0                   0                   0                   0   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "5                   0                   0                   0   \n",
       "6                   0                   0                   0   \n",
       "7                   0                   0                   0   \n",
       "8                   0                   0                   0   \n",
       "9                   0                   0                   0   \n",
       "\n",
       "   genre_ids_matrix_6  genre_ids_matrix_7  genre_ids_matrix_8  \\\n",
       "0                   0                   0                   0   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "5                   0                   0                   0   \n",
       "6                   0                   0                   0   \n",
       "7                   0                   0                   0   \n",
       "8                   0                   0                   0   \n",
       "9                   0                   0                   0   \n",
       "\n",
       "   genre_ids_matrix_9  ...  genre_ids_matrix_182  genre_ids_matrix_183  \\\n",
       "0                   0  ...                     0                     0   \n",
       "1                   0  ...                     0                     0   \n",
       "2                   0  ...                     0                     0   \n",
       "3                   0  ...                     0                     0   \n",
       "4                   0  ...                     0                     0   \n",
       "5                   0  ...                     0                     0   \n",
       "6                   0  ...                     0                     0   \n",
       "7                   0  ...                     0                     0   \n",
       "8                   0  ...                     0                     0   \n",
       "9                   0  ...                     0                     0   \n",
       "\n",
       "   genre_ids_matrix_184  genre_ids_matrix_185  genre_ids_matrix_186  \\\n",
       "0                     0                     0                     0   \n",
       "1                     0                     0                     0   \n",
       "2                     0                     0                     0   \n",
       "3                     0                     0                     0   \n",
       "4                     0                     0                     0   \n",
       "5                     0                     0                     0   \n",
       "6                     0                     0                     0   \n",
       "7                     0                     0                     0   \n",
       "8                     0                     0                     0   \n",
       "9                     0                     0                     0   \n",
       "\n",
       "   genre_ids_matrix_187  genre_ids_matrix_188  genre_ids_matrix_189  \\\n",
       "0                     0                     0                     0   \n",
       "1                     0                     0                     0   \n",
       "2                     0                     0                     0   \n",
       "3                     0                     0                     0   \n",
       "4                     0                     0                     0   \n",
       "5                     0                     0                     0   \n",
       "6                     0                     0                     0   \n",
       "7                     0                     0                     0   \n",
       "8                     0                     0                     0   \n",
       "9                     0                     0                     0   \n",
       "\n",
       "   genre_ids_matrix_190  genre_ids_matrix_191  \n",
       "0                     0                     0  \n",
       "1                     0                     0  \n",
       "2                     0                     0  \n",
       "3                     0                     0  \n",
       "4                     0                     0  \n",
       "5                     0                     0  \n",
       "6                     0                     0  \n",
       "7                     0                     0  \n",
       "8                     0                     0  \n",
       "9                     0                     0  \n",
       "\n",
       "[10 rows x 192 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_count_matrix = pd.DataFrame(columns= column_name, data = genre_ids_V.toarray())\n",
    "df_count_matrix.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （2）language特征处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看language数据分布情况\n",
    "language = df_songs_new_dataset['language'].value_counts() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       " 52.0    1337018\n",
       "-1.0      639616\n",
       " 3.0      106302\n",
       " 17.0      92532\n",
       " 24.0      41748\n",
       " 31.0      39208\n",
       " 10.0      15485\n",
       " 45.0      14436\n",
       " 59.0       8101\n",
       " 38.0       2387\n",
       "Name: language, dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "language"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **language是属于类别型特征，所以直接onehot编码**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "language_cat = df_songs_new_dataset['language']\n",
    "language_cat = pd.get_dummies(language_cat, prefix=\"language\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>language_-1.0</th>\n",
       "      <th>language_3.0</th>\n",
       "      <th>language_10.0</th>\n",
       "      <th>language_17.0</th>\n",
       "      <th>language_24.0</th>\n",
       "      <th>language_31.0</th>\n",
       "      <th>language_38.0</th>\n",
       "      <th>language_45.0</th>\n",
       "      <th>language_52.0</th>\n",
       "      <th>language_59.0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   language_-1.0  language_3.0  language_10.0  language_17.0  language_24.0  \\\n",
       "0              0             1              0              0              0   \n",
       "1              0             0              0              0              0   \n",
       "2              0             0              0              0              0   \n",
       "3              0             1              0              0              0   \n",
       "4              0             0              0              0              0   \n",
       "\n",
       "   language_31.0  language_38.0  language_45.0  language_52.0  language_59.0  \n",
       "0              0              0              0              0              0  \n",
       "1              1              0              0              0              0  \n",
       "2              1              0              0              0              0  \n",
       "3              0              0              0              0              0  \n",
       "4              0              0              0              1              0  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "language_cat.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （3）artist_name特征处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 查看artist_name数据分布情况\n",
    "artist_name = df_songs_new_dataset['artist_name'].value_counts() "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Various Artists                                                                 145951\n",
       "証聲音樂圖書館 ECHO MUSIC                                                               11278\n",
       "Billy Vaughn                                                                      4828\n",
       "รวมศิลปิน                                                                         4432\n",
       "Richard Clayderman                                                                4181\n",
       "Elvis Presley                                                                     4039\n",
       "Nat King Cole                                                                     3807\n",
       "Billie Holiday                                                                    3674\n",
       "Frank Sinatra                                                                     3596\n",
       "Armin van Buuren                                                                  2819\n",
       "Various                                                                           2743\n",
       "Ella Fitzgerald                                                                   2721\n",
       "オルゴールサウンド J-POP                                                                   2693\n",
       "Glenn Gould                                                                       2690\n",
       "Bill Evans                                                                        2572\n",
       "Chet Baker                                                                        2533\n",
       "鄧麗君 (Teresa Teng)                                                                 2487\n",
       "Bob Dylan                                                                         2471\n",
       "貴族精選                                                                              2419\n",
       "Ray Charles                                                                       2236\n",
       "The Kiboomers                                                                     2230\n",
       "Michael Jackson                                                                   2208\n",
       "Miles Davis                                                                       2179\n",
       "Falcom Sound Team jdk                                                             2165\n",
       "Louis Armstrong                                                                   2161\n",
       "Wolfgang Amadeus Mozart                                                           2048\n",
       "Andy Williams                                                                     1995\n",
       "Power Music Workout                                                               1987\n",
       "霹靂布袋戲劇集原聲帶                                                                        1905\n",
       "Julie London                                                                      1840\n",
       "                                                                                 ...  \n",
       "Cloakroom                                                                            1\n",
       "Hector Cantú                                                                         1\n",
       "Dominik Ofner & Die Band                                                             1\n",
       "Allan Caswell                                                                        1\n",
       "Douglas Jacoby                                                                       1\n",
       "Less                                                                                 1\n",
       "We Sing U Sing                                                                       1\n",
       "The Coathangers / Audacity                                                           1\n",
       "Traitors                                                                             1\n",
       "Peeruram Bhopa| Rooparam Bhopa                                                       1\n",
       "Zimbra                                                                               1\n",
       "Stéphane Océane                                                                      1\n",
       "Heart Strings with Dunn Pearson| Jr.                                                 1\n",
       "Rekrutenspiel Schweizer Militärmusik - Brass Band & Oblt Philipp Werlen              1\n",
       "Trikot                                                                               1\n",
       "Wuji (Turner| Maloney & Gresch)                                                      1\n",
       "The Foibles                                                                          1\n",
       "정형돈|데프콘|JD                                                                           1\n",
       "Upinatem                                                                             1\n",
       "Julión Álvarez Y Su Norteño Banda                                                    1\n",
       "Mstislav Rostropovich|Martha Argerich|National Symphony Orchestra Washington         1\n",
       "John Schumann                                                                        1\n",
       "Warren Burns                                                                         1\n",
       "クレイジーケン                                                                              1\n",
       "Catweazle                                                                            1\n",
       "Sydney Symphony Orchestra                                                            1\n",
       "Nord                                                                                 1\n",
       "Peter Christie                                                                       1\n",
       "Egil Eldøen                                                                          1\n",
       "P. Jayachandran                                                                      1\n",
       "Name: artist_name, Length: 222408, dtype: int64"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "artist_name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **由于artist_name都是字符类型，所以转换为LabelDecoder方便查询和可视化**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn import preprocessing\n",
    "le = preprocessing.LabelEncoder()\n",
    "artist_name_le = le.fit_transform(df_songs_new_dataset['artist_name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(2296833,)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "artist_name_le.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>artist_name_label</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>214773</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>16525</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>157070</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>156287</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>219501</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>219501</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>212267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>212543</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>216212</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>100009</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   artist_name_label\n",
       "0             214773\n",
       "1              16525\n",
       "2             157070\n",
       "3             156287\n",
       "4             219501\n",
       "5             219501\n",
       "6             212267\n",
       "7             212543\n",
       "8             216212\n",
       "9             100009"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_artist_name_label = pd.DataFrame(data=artist_name_le, columns=['artist_name_label'])\n",
    "df_artist_name_label.head(10)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.3、合并&保存特征工程结果到文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把上面的特征工程合并在一起\n",
    "fe_song_id_data = pd.DataFrame(data = df_songs_new_dataset['song_id'], columns = ['song_id'], index = df_songs_new_dataset.index)\n",
    "fe_song_data = pd.concat([fe_song_id_data, df_count_matrix, df_artist_name_label, language_cat], axis = 1, ignore_index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>song_id</th>\n",
       "      <th>genre_ids_matrix_0</th>\n",
       "      <th>genre_ids_matrix_1</th>\n",
       "      <th>genre_ids_matrix_2</th>\n",
       "      <th>genre_ids_matrix_3</th>\n",
       "      <th>genre_ids_matrix_4</th>\n",
       "      <th>genre_ids_matrix_5</th>\n",
       "      <th>genre_ids_matrix_6</th>\n",
       "      <th>genre_ids_matrix_7</th>\n",
       "      <th>genre_ids_matrix_8</th>\n",
       "      <th>...</th>\n",
       "      <th>language_-1.0</th>\n",
       "      <th>language_3.0</th>\n",
       "      <th>language_10.0</th>\n",
       "      <th>language_17.0</th>\n",
       "      <th>language_24.0</th>\n",
       "      <th>language_31.0</th>\n",
       "      <th>language_38.0</th>\n",
       "      <th>language_45.0</th>\n",
       "      <th>language_52.0</th>\n",
       "      <th>language_59.0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>CXoTN1eb7AI+DntdU1vbcwGRV4SCIDxZu+YD8JP8r4E=</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>o0kFgae9QtnYgRkVPqLJwa05zIhRlUjfF7O1tDw0ZDU=</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>DwVvVurfpuz+XPuFvucclVQEyPqcpUkHR0ne1RQzPs0=</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>dKMBWoZyScdxSkihKG+Vf47nc18N9q4m58+b4e7dSSE=</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>W3bqWd3T+VeHFzHAUfARgW9AvVRaF4N5Yzm4Mr6Eo/o=</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 204 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                        song_id  genre_ids_matrix_0  \\\n",
       "0  CXoTN1eb7AI+DntdU1vbcwGRV4SCIDxZu+YD8JP8r4E=                   0   \n",
       "1  o0kFgae9QtnYgRkVPqLJwa05zIhRlUjfF7O1tDw0ZDU=                   0   \n",
       "2  DwVvVurfpuz+XPuFvucclVQEyPqcpUkHR0ne1RQzPs0=                   0   \n",
       "3  dKMBWoZyScdxSkihKG+Vf47nc18N9q4m58+b4e7dSSE=                   0   \n",
       "4  W3bqWd3T+VeHFzHAUfARgW9AvVRaF4N5Yzm4Mr6Eo/o=                   0   \n",
       "\n",
       "   genre_ids_matrix_1  genre_ids_matrix_2  genre_ids_matrix_3  \\\n",
       "0                   0                   0                   0   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "\n",
       "   genre_ids_matrix_4  genre_ids_matrix_5  genre_ids_matrix_6  \\\n",
       "0                   0                   0                   0   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "\n",
       "   genre_ids_matrix_7  genre_ids_matrix_8  ...  language_-1.0  language_3.0  \\\n",
       "0                   0                   0  ...              0             1   \n",
       "1                   0                   0  ...              0             0   \n",
       "2                   0                   0  ...              0             0   \n",
       "3                   0                   0  ...              0             1   \n",
       "4                   0                   0  ...              0             0   \n",
       "\n",
       "   language_10.0  language_17.0  language_24.0  language_31.0  language_38.0  \\\n",
       "0              0              0              0              0              0   \n",
       "1              0              0              0              1              0   \n",
       "2              0              0              0              1              0   \n",
       "3              0              0              0              0              0   \n",
       "4              0              0              0              0              0   \n",
       "\n",
       "   language_45.0  language_52.0  language_59.0  \n",
       "0              0              0              0  \n",
       "1              0              0              0  \n",
       "2              0              0              0  \n",
       "3              0              0              0  \n",
       "4              0              1              0  \n",
       "\n",
       "[5 rows x 204 columns]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fe_song_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存结果到文件（保存完后有1.05GB！）\n",
    "fe_song_data.to_csv(dpath + 'LR_data/FE_Songs.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、members.csv特征工程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>20110820</td>\n",
       "      <td>20170920</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>20150628</td>\n",
       "      <td>20170622</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>20160411</td>\n",
       "      <td>20170712</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>20150906</td>\n",
       "      <td>20150907</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>20170126</td>\n",
       "      <td>20170613</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0    NaN   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0    NaN   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0    NaN   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0    NaN   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0    NaN   \n",
       "\n",
       "   registered_via  registration_init_time  expiration_date  \n",
       "0               7                20110820         20170920  \n",
       "1               7                20150628         20170622  \n",
       "2               4                20160411         20170712  \n",
       "3               9                20150906         20150907  \n",
       "4               4                20170126         20170613  "
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "text/plain": [
       "(34403, 7)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset = pd.read_csv(dpath +'members.csv')\n",
    "df_members_new_dataset.head()\n",
    "df_members_new_dataset.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 34403 entries, 0 to 34402\n",
      "Data columns (total 7 columns):\n",
      "msno                      34403 non-null object\n",
      "city                      34403 non-null int64\n",
      "bd                        34403 non-null int64\n",
      "gender                    14501 non-null object\n",
      "registered_via            34403 non-null int64\n",
      "registration_init_time    34403 non-null int64\n",
      "expiration_date           34403 non-null int64\n",
      "dtypes: int64(5), object(2)\n",
      "memory usage: 1.8+ MB\n"
     ]
    }
   ],
   "source": [
    "df_members_new_dataset.info()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.1、查看每个特征的缺失值数量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "msno                          0\n",
       "city                          0\n",
       "bd                            0\n",
       "gender                    19902\n",
       "registered_via                0\n",
       "registration_init_time        0\n",
       "expiration_date               0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* **gender（性别）近一半数据缺失，有2种填补方式：**\n",
    "**（1）新增2列：gender_male和gender_female。如果是male，则编码：1,0，如果是female，则编码：0,1，如果是缺失值，则编码：0,0**\n",
    "**（2）把缺失值按50%随机填充男，另外50%填充女。因为原始数据中的男女也不一定代表真正意义的男女，有些女性在社交网络上就会填男性，而有些男性则会填女性。而且一个人要么是男性，要么是女性，都是1/2的概率，所以随机按50%填充男性和女性也是比较合理的。**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.2、查看各特征分布情况"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "city属性有21种不同取值，各取值及其出现的次数\n",
      "\n",
      "1     19445\n",
      "13     3395\n",
      "5      2634\n",
      "4      1732\n",
      "15     1525\n",
      "22     1467\n",
      "6       913\n",
      "14      708\n",
      "12      491\n",
      "9       309\n",
      "8       289\n",
      "11      285\n",
      "18      259\n",
      "10      216\n",
      "21      213\n",
      "3       204\n",
      "17      152\n",
      "7        93\n",
      "16       35\n",
      "20       27\n",
      "19       11\n",
      "Name: city, dtype: int64\n",
      "\n",
      "bd属性有95种不同取值，各取值及其出现的次数\n",
      "\n",
      " 0       19932\n",
      " 22        751\n",
      " 27        750\n",
      " 24        740\n",
      " 26        719\n",
      " 25        716\n",
      " 23        712\n",
      " 28        688\n",
      " 21        685\n",
      " 29        661\n",
      " 20        631\n",
      " 30        602\n",
      " 19        507\n",
      " 31        491\n",
      " 32        466\n",
      " 18        466\n",
      " 33        416\n",
      " 34        404\n",
      " 17        398\n",
      " 35        380\n",
      " 36        341\n",
      " 37        300\n",
      " 38        294\n",
      " 39        226\n",
      " 16        215\n",
      " 40        204\n",
      " 41        194\n",
      " 44        138\n",
      " 42        131\n",
      " 43        121\n",
      "         ...  \n",
      " 102         2\n",
      " 131         1\n",
      " 78          1\n",
      " 85          1\n",
      " 3           1\n",
      " 2           1\n",
      " 1051        1\n",
      " 97          1\n",
      " 144         1\n",
      " 93          1\n",
      " 96          1\n",
      "-43          1\n",
      " 82          1\n",
      " 931         1\n",
      " 106         1\n",
      " 76          1\n",
      " 87          1\n",
      " 101         1\n",
      " 90          1\n",
      " 70          1\n",
      " 1030        1\n",
      " 7           1\n",
      " 12          1\n",
      " 103         1\n",
      "-38          1\n",
      " 89          1\n",
      " 107         1\n",
      " 10          1\n",
      " 11          1\n",
      " 95          1\n",
      "Name: bd, Length: 95, dtype: int64\n",
      "\n",
      "gender属性有3种不同取值，各取值及其出现的次数\n",
      "\n",
      "male      7405\n",
      "female    7096\n",
      "Name: gender, dtype: int64\n",
      "\n",
      "registered_via属性有6种不同取值，各取值及其出现的次数\n",
      "\n",
      "4     11392\n",
      "7      9433\n",
      "9      8628\n",
      "3      4879\n",
      "13       70\n",
      "16        1\n",
      "Name: registered_via, dtype: int64\n",
      "\n",
      "registration_init_time属性有3862种不同取值，各取值及其出现的次数\n",
      "\n",
      "20170126    117\n",
      "20161203    116\n",
      "20161231    113\n",
      "20170122    111\n",
      "20170121    109\n",
      "20170102    107\n",
      "20161217    106\n",
      "20161204    106\n",
      "20161224    104\n",
      "20170120    104\n",
      "20170204    103\n",
      "20161211    103\n",
      "20161218    102\n",
      "20170127    100\n",
      "20170108     99\n",
      "20170107     98\n",
      "20161225     98\n",
      "20170131     98\n",
      "20161202     97\n",
      "20161210     95\n",
      "20170202     94\n",
      "20170130     94\n",
      "20161212     93\n",
      "20170227     93\n",
      "20161227     93\n",
      "20170203     92\n",
      "20170114     92\n",
      "20170225     91\n",
      "20161230     91\n",
      "20170207     90\n",
      "           ... \n",
      "20091223      1\n",
      "20071213      1\n",
      "20080510      1\n",
      "20070101      1\n",
      "20060721      1\n",
      "20090720      1\n",
      "20080605      1\n",
      "20121029      1\n",
      "20080703      1\n",
      "20091008      1\n",
      "20070618      1\n",
      "20091104      1\n",
      "20060401      1\n",
      "20050316      1\n",
      "20060625      1\n",
      "20050412      1\n",
      "20050828      1\n",
      "20090531      1\n",
      "20040828      1\n",
      "20040619      1\n",
      "20050924      1\n",
      "20040715      1\n",
      "20051003      1\n",
      "20110409      1\n",
      "20100115      1\n",
      "20051212      1\n",
      "20050907      1\n",
      "20041227      1\n",
      "20040604      1\n",
      "20080627      1\n",
      "Name: registration_init_time, Length: 3862, dtype: int64\n",
      "\n",
      "expiration_date属性有1484种不同取值，各取值及其出现的次数\n",
      "\n",
      "20170930    2498\n",
      "20171004     551\n",
      "20171005     549\n",
      "20171006     515\n",
      "20171001     495\n",
      "20171002     494\n",
      "20170917     481\n",
      "20170916     480\n",
      "20171003     460\n",
      "20170920     447\n",
      "20170910     447\n",
      "20170919     433\n",
      "20170909     427\n",
      "20170912     416\n",
      "20170921     412\n",
      "20170922     412\n",
      "20170913     412\n",
      "20170918     406\n",
      "20170923     399\n",
      "20170908     396\n",
      "20170911     392\n",
      "20170924     388\n",
      "20171007     386\n",
      "20170915     378\n",
      "20170914     372\n",
      "20170708     255\n",
      "20170925     235\n",
      "20170609     214\n",
      "20170610     196\n",
      "20170612     195\n",
      "            ... \n",
      "20151231       1\n",
      "20110219       1\n",
      "20150301       1\n",
      "20140504       1\n",
      "20160626       1\n",
      "20150813       1\n",
      "20181112       1\n",
      "20090210       1\n",
      "20120819       1\n",
      "20181208       1\n",
      "20150505       1\n",
      "20120723       1\n",
      "20151004       1\n",
      "20150908       1\n",
      "20140601       1\n",
      "20150812       1\n",
      "20140409       1\n",
      "20060709       1\n",
      "20120102       1\n",
      "20150524       1\n",
      "20181101       1\n",
      "20121106       1\n",
      "20120722       1\n",
      "20140908       1\n",
      "20140824       1\n",
      "20130915       1\n",
      "20190308       1\n",
      "20151005       1\n",
      "20131011       1\n",
      "20191229       1\n",
      "Name: expiration_date, Length: 1484, dtype: int64\n"
     ]
    }
   ],
   "source": [
    "cat_features = ['city','bd','gender','registered_via','registration_init_time','expiration_date']\n",
    "for col in cat_features:\n",
    "    num_vlaules = len(df_members_new_dataset[col].unique())\n",
    "    print('\\n%s属性有%d种不同取值，各取值及其出现的次数\\n'% (col,num_vlaules))\n",
    "    print(df_members_new_dataset[col].value_counts())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- city没有缺失值，而且是类别特征，可以进行onehot编码\n",
    "- bd（年龄）有太多为0的数据了，这些数据都属于异常值，所以考虑新增一列：是否异常，0代表正常，1代表异常\n",
    "- gender（性别）缺失值也很多，暂时考虑上面的第2种方式填补缺失值\n",
    "- registered_via（注册方式）没有缺失值，也属于类别特征，直接onehot编码即可\n",
    "- registration_init_time（注册时间）和expiration_date（到期时间）可以用到期时间-注册时间计算用户时长，然后再分段：当天，5天，10天，1个月，半年，1年，2年，3年，5年，7年，10年，超过10年"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.3、分别对每个特征预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （1）city特征处理\n",
    "\n",
    "**因为city是属于类别特征，所以直接进行onehot编码即可**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "city_cat = df_members_new_dataset['city']\n",
    "city_cat = pd.get_dummies(city_cat, prefix=\"city\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>city_1</th>\n",
       "      <th>city_3</th>\n",
       "      <th>city_4</th>\n",
       "      <th>city_5</th>\n",
       "      <th>city_6</th>\n",
       "      <th>city_7</th>\n",
       "      <th>city_8</th>\n",
       "      <th>city_9</th>\n",
       "      <th>city_10</th>\n",
       "      <th>city_11</th>\n",
       "      <th>...</th>\n",
       "      <th>city_13</th>\n",
       "      <th>city_14</th>\n",
       "      <th>city_15</th>\n",
       "      <th>city_16</th>\n",
       "      <th>city_17</th>\n",
       "      <th>city_18</th>\n",
       "      <th>city_19</th>\n",
       "      <th>city_20</th>\n",
       "      <th>city_21</th>\n",
       "      <th>city_22</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   city_1  city_3  city_4  city_5  city_6  city_7  city_8  city_9  city_10  \\\n",
       "0       1       0       0       0       0       0       0       0        0   \n",
       "1       1       0       0       0       0       0       0       0        0   \n",
       "2       1       0       0       0       0       0       0       0        0   \n",
       "3       1       0       0       0       0       0       0       0        0   \n",
       "4       1       0       0       0       0       0       0       0        0   \n",
       "\n",
       "   city_11  ...  city_13  city_14  city_15  city_16  city_17  city_18  \\\n",
       "0        0  ...        0        0        0        0        0        0   \n",
       "1        0  ...        0        0        0        0        0        0   \n",
       "2        0  ...        0        0        0        0        0        0   \n",
       "3        0  ...        0        0        0        0        0        0   \n",
       "4        0  ...        0        0        0        0        0        0   \n",
       "\n",
       "   city_19  city_20  city_21  city_22  \n",
       "0        0        0        0        0  \n",
       "1        0        0        0        0  \n",
       "2        0        0        0        0  \n",
       "3        0        0        0        0  \n",
       "4        0        0        0        0  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "city_cat.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （2）bd特征处理\n",
    "\n",
    "**新增一列：是否异常，0代表正常，1代表异常**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_members_new_dataset['bd_exception'] = df_members_new_dataset['bd'].apply(lambda x: 1 if (pd.isnull(x) or x==0) else 0)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>bd_exception</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>20110820</td>\n",
       "      <td>20170920</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>20150628</td>\n",
       "      <td>20170622</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>20160411</td>\n",
       "      <td>20170712</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>20150906</td>\n",
       "      <td>20150907</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>20170126</td>\n",
       "      <td>20170613</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0    NaN   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0    NaN   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0    NaN   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0    NaN   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0    NaN   \n",
       "\n",
       "   registered_via  registration_init_time  expiration_date  bd_exception  \n",
       "0               7                20110820         20170920             1  \n",
       "1               7                20150628         20170622             1  \n",
       "2               4                20160411         20170712             1  \n",
       "3               9                20150906         20150907             1  \n",
       "4               4                20170126         20170613             1  "
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （3）gender特征处理\n",
    "\n",
    "**缺失值采用随机50%填补男性，另外50%填补女性**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 随机50%的缺失值，设置为male\n",
    "df_members_new_dataset.loc[\n",
    "    df_members_new_dataset.query('gender.isnull()').sample(frac=.5).index,\n",
    "    'gender'\n",
    "] = 'male'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>bd_exception</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>20110820</td>\n",
       "      <td>20170920</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>7</td>\n",
       "      <td>20150628</td>\n",
       "      <td>20170622</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "      <td>20160411</td>\n",
       "      <td>20170712</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9</td>\n",
       "      <td>20150906</td>\n",
       "      <td>20150907</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>male</td>\n",
       "      <td>4</td>\n",
       "      <td>20170126</td>\n",
       "      <td>20170613</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0    NaN   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0    NaN   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0    NaN   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0    NaN   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0   male   \n",
       "\n",
       "   registered_via  registration_init_time  expiration_date  bd_exception  \n",
       "0               7                20110820         20170920             1  \n",
       "1               7                20150628         20170622             1  \n",
       "2               4                20160411         20170712             1  \n",
       "3               9                20150906         20150907             1  \n",
       "4               4                20170126         20170613             1  "
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "msno                         0\n",
       "city                         0\n",
       "bd                           0\n",
       "gender                    9951\n",
       "registered_via               0\n",
       "registration_init_time       0\n",
       "expiration_date              0\n",
       "bd_exception                 0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证一下上面的方法是否正确\n",
    "df_members_new_dataset.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**由此看出，随机抽样50%的缺失值，填补为male是有效的。填补之前是缺失值是：19902，填补50%后，剩余9951**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 剩下的缺失值全填补为female\n",
    "df_members_new_dataset.loc[\n",
    "    df_members_new_dataset.query('gender.isnull()').index,\n",
    "    'gender'\n",
    "] = 'female'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>bd_exception</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>female</td>\n",
       "      <td>7</td>\n",
       "      <td>20110820</td>\n",
       "      <td>20170920</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>female</td>\n",
       "      <td>7</td>\n",
       "      <td>20150628</td>\n",
       "      <td>20170622</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>female</td>\n",
       "      <td>4</td>\n",
       "      <td>20160411</td>\n",
       "      <td>20170712</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>female</td>\n",
       "      <td>9</td>\n",
       "      <td>20150906</td>\n",
       "      <td>20150907</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>male</td>\n",
       "      <td>4</td>\n",
       "      <td>20170126</td>\n",
       "      <td>20170613</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd  gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0  female   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0  female   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0  female   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0  female   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0    male   \n",
       "\n",
       "   registered_via  registration_init_time  expiration_date  bd_exception  \n",
       "0               7                20110820         20170920             1  \n",
       "1               7                20150628         20170622             1  \n",
       "2               4                20160411         20170712             1  \n",
       "3               9                20150906         20150907             1  \n",
       "4               4                20170126         20170613             1  "
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "msno                      0\n",
       "city                      0\n",
       "bd                        0\n",
       "gender                    0\n",
       "registered_via            0\n",
       "registration_init_time    0\n",
       "expiration_date           0\n",
       "bd_exception              0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 验证一下上面的方法是否正确\n",
    "df_members_new_dataset.isnull().sum()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**全部填充完毕，下面对字符串'female'和'male'编码成2和1，方便计算**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 'female'和'male'编码成2和1\n",
    "df_members_new_dataset['gender'] = df_members_new_dataset['gender'].apply(lambda x: 2 if (x=='female') else 1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>bd_exception</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>20110820</td>\n",
       "      <td>20170920</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>20150628</td>\n",
       "      <td>20170622</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>20160411</td>\n",
       "      <td>20170712</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>20150906</td>\n",
       "      <td>20150907</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>20170126</td>\n",
       "      <td>20170613</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd  gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0       2   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0       2   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0       2   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0       2   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0       1   \n",
       "\n",
       "   registered_via  registration_init_time  expiration_date  bd_exception  \n",
       "0               7                20110820         20170920             1  \n",
       "1               7                20150628         20170622             1  \n",
       "2               4                20160411         20170712             1  \n",
       "3               9                20150906         20150907             1  \n",
       "4               4                20170126         20170613             1  "
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （4）registered_via特征处理\n",
    "\n",
    "**因为registered_via是属于类别特征，所以直接进行onehot编码即可**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "registered_via_cat = df_members_new_dataset['registered_via']\n",
    "registered_via_cat = pd.get_dummies(registered_via_cat, prefix=\"registered_via\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>registered_via_3</th>\n",
       "      <th>registered_via_4</th>\n",
       "      <th>registered_via_7</th>\n",
       "      <th>registered_via_9</th>\n",
       "      <th>registered_via_13</th>\n",
       "      <th>registered_via_16</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   registered_via_3  registered_via_4  registered_via_7  registered_via_9  \\\n",
       "0                 0                 0                 1                 0   \n",
       "1                 0                 0                 1                 0   \n",
       "2                 0                 1                 0                 0   \n",
       "3                 0                 0                 0                 1   \n",
       "4                 0                 1                 0                 0   \n",
       "\n",
       "   registered_via_13  registered_via_16  \n",
       "0                  0                  0  \n",
       "1                  0                  0  \n",
       "2                  0                  0  \n",
       "3                  0                  0  \n",
       "4                  0                  0  "
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "registered_via_cat.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### （5）registration_init_time和expiration_date特征处理"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **先用expiration_date-registration_init_time计算出时长**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "\n",
    "# 先将时间转换为str，然后再转换成日期类型，进行相减操作\n",
    "df_members_new_dataset['day_diff'] = pd.to_datetime(df_members_new_dataset['expiration_date'].map(lambda x : str(x))) - pd.to_datetime(df_members_new_dataset['registration_init_time'].map(lambda x : str(x)))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>registration_init_time</th>\n",
       "      <th>expiration_date</th>\n",
       "      <th>bd_exception</th>\n",
       "      <th>day_diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>20110820</td>\n",
       "      <td>20170920</td>\n",
       "      <td>1</td>\n",
       "      <td>2223 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>20150628</td>\n",
       "      <td>20170622</td>\n",
       "      <td>1</td>\n",
       "      <td>725 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>20160411</td>\n",
       "      <td>20170712</td>\n",
       "      <td>1</td>\n",
       "      <td>457 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>20150906</td>\n",
       "      <td>20150907</td>\n",
       "      <td>1</td>\n",
       "      <td>1 days</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>20170126</td>\n",
       "      <td>20170613</td>\n",
       "      <td>1</td>\n",
       "      <td>138 days</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd  gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0       2   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0       2   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0       2   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0       2   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0       1   \n",
       "\n",
       "   registered_via  registration_init_time  expiration_date  bd_exception  \\\n",
       "0               7                20110820         20170920             1   \n",
       "1               7                20150628         20170622             1   \n",
       "2               4                20160411         20170712             1   \n",
       "3               9                20150906         20150907             1   \n",
       "4               4                20170126         20170613             1   \n",
       "\n",
       "   day_diff  \n",
       "0 2223 days  \n",
       "1  725 days  \n",
       "2  457 days  \n",
       "3    1 days  \n",
       "4  138 days  "
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_members_new_dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 减出来的只保留时间\n",
    "df_members_new_dataset['day_diff'] = df_members_new_dataset['day_diff'].map(lambda x : int(str(x)[:-13])) \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x126a4afd0>"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "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\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "df_members_new_dataset['day_diff'].hist()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city</th>\n",
       "      <th>bd</th>\n",
       "      <th>gender</th>\n",
       "      <th>registered_via</th>\n",
       "      <th>bd_exception</th>\n",
       "      <th>day_diff</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>2223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>457</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>138</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city  bd  gender  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=     1   0       2   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=     1   0       2   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=     1   0       2   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=     1   0       2   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=     1   0       1   \n",
       "\n",
       "   registered_via  bd_exception  day_diff  \n",
       "0               7             1      2223  \n",
       "1               7             1       725  \n",
       "2               4             1       457  \n",
       "3               9             1         1  \n",
       "4               4             1       138  "
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除这2个时间列\n",
    "df_members_new_dataset = df_members_new_dataset.drop(['registration_init_time', 'expiration_date'], axis=1)\n",
    "df_members_new_dataset.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **对day_diff进行分段离散化**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 当天，5天，10天，1个月，半年，1年，2年，3年，5年，7年，10年，超过10年\n",
    "date_diff_bin = [-1, 0, 5, 10, 30, 183, 365, 730, 1095, 1825, 2555, 3650, 99999] # cut区间默认是“左开右闭”，所以取当天=(-1,0]\n",
    "df_members_new_dataset['date_diff_bin'] = pd.cut(df_members_new_dataset['day_diff'], date_diff_bin, labels = [0, 5, 10, 30, 183, 365, 730, 1095, 1825, 2555, 3650, 99999])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5        6492\n",
       "1825     5316\n",
       "730      5088\n",
       "2555     4807\n",
       "1095     3098\n",
       "365      3070\n",
       "3650     1828\n",
       "99999    1682\n",
       "10       1264\n",
       "183      1177\n",
       "0         292\n",
       "30        289\n",
       "Name: date_diff_bin, dtype: int64"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查看各个区间的取值数量\n",
    "pd.value_counts(df_members_new_dataset['date_diff_bin'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- **day_diff离散化后再进行onehot编码**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [],
   "source": [
    "date_diff_bin_cat = df_members_new_dataset['date_diff_bin']\n",
    "date_diff_bin_cat = pd.get_dummies(date_diff_bin_cat, prefix=\"date_diff_bin\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>date_diff_bin_0</th>\n",
       "      <th>date_diff_bin_5</th>\n",
       "      <th>date_diff_bin_10</th>\n",
       "      <th>date_diff_bin_30</th>\n",
       "      <th>date_diff_bin_183</th>\n",
       "      <th>date_diff_bin_365</th>\n",
       "      <th>date_diff_bin_730</th>\n",
       "      <th>date_diff_bin_1095</th>\n",
       "      <th>date_diff_bin_1825</th>\n",
       "      <th>date_diff_bin_2555</th>\n",
       "      <th>date_diff_bin_3650</th>\n",
       "      <th>date_diff_bin_99999</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   date_diff_bin_0  date_diff_bin_5  date_diff_bin_10  date_diff_bin_30  \\\n",
       "0                0                0                 0                 0   \n",
       "1                0                0                 0                 0   \n",
       "2                0                0                 0                 0   \n",
       "3                0                1                 0                 0   \n",
       "4                0                0                 0                 0   \n",
       "\n",
       "   date_diff_bin_183  date_diff_bin_365  date_diff_bin_730  \\\n",
       "0                  0                  0                  0   \n",
       "1                  0                  0                  1   \n",
       "2                  0                  0                  1   \n",
       "3                  0                  0                  0   \n",
       "4                  1                  0                  0   \n",
       "\n",
       "   date_diff_bin_1095  date_diff_bin_1825  date_diff_bin_2555  \\\n",
       "0                   0                   0                   1   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "\n",
       "   date_diff_bin_3650  date_diff_bin_99999  \n",
       "0                   0                    0  \n",
       "1                   0                    0  \n",
       "2                   0                    0  \n",
       "3                   0                    0  \n",
       "4                   0                    0  "
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date_diff_bin_cat.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.4、合并&保存特征工程结果到文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把上面的特征工程合并在一起\n",
    "fe_msno_data = pd.DataFrame(data = df_members_new_dataset['msno'], columns = ['msno'], index = df_members_new_dataset.index)\n",
    "fe_member_data = pd.concat([fe_msno_data, city_cat, df_members_new_dataset['bd_exception'], df_members_new_dataset['gender'], registered_via_cat, date_diff_bin_cat], axis = 1, ignore_index=False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>msno</th>\n",
       "      <th>city_1</th>\n",
       "      <th>city_3</th>\n",
       "      <th>city_4</th>\n",
       "      <th>city_5</th>\n",
       "      <th>city_6</th>\n",
       "      <th>city_7</th>\n",
       "      <th>city_8</th>\n",
       "      <th>city_9</th>\n",
       "      <th>city_10</th>\n",
       "      <th>...</th>\n",
       "      <th>date_diff_bin_10</th>\n",
       "      <th>date_diff_bin_30</th>\n",
       "      <th>date_diff_bin_183</th>\n",
       "      <th>date_diff_bin_365</th>\n",
       "      <th>date_diff_bin_730</th>\n",
       "      <th>date_diff_bin_1095</th>\n",
       "      <th>date_diff_bin_1825</th>\n",
       "      <th>date_diff_bin_2555</th>\n",
       "      <th>date_diff_bin_3650</th>\n",
       "      <th>date_diff_bin_99999</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 42 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                           msno  city_1  city_3  city_4  \\\n",
       "0  XQxgAYj3klVKjR3oxPPXYYFp4soD4TuBghkhMTD4oTw=       1       0       0   \n",
       "1  UizsfmJb9mV54qE9hCYyU07Va97c0lCRLEQX3ae+ztM=       1       0       0   \n",
       "2  D8nEhsIOBSoE6VthTaqDX8U6lqjJ7dLdr72mOyLya2A=       1       0       0   \n",
       "3  mCuD+tZ1hERA/o5GPqk38e041J8ZsBaLcu7nGoIIvhI=       1       0       0   \n",
       "4  q4HRBfVSssAFS9iRfxWrohxuk9kCYMKjHOEagUMV6rQ=       1       0       0   \n",
       "\n",
       "   city_5  city_6  city_7  city_8  city_9  city_10  ...  date_diff_bin_10  \\\n",
       "0       0       0       0       0       0        0  ...                 0   \n",
       "1       0       0       0       0       0        0  ...                 0   \n",
       "2       0       0       0       0       0        0  ...                 0   \n",
       "3       0       0       0       0       0        0  ...                 0   \n",
       "4       0       0       0       0       0        0  ...                 0   \n",
       "\n",
       "   date_diff_bin_30  date_diff_bin_183  date_diff_bin_365  date_diff_bin_730  \\\n",
       "0                 0                  0                  0                  0   \n",
       "1                 0                  0                  0                  1   \n",
       "2                 0                  0                  0                  1   \n",
       "3                 0                  0                  0                  0   \n",
       "4                 0                  1                  0                  0   \n",
       "\n",
       "   date_diff_bin_1095  date_diff_bin_1825  date_diff_bin_2555  \\\n",
       "0                   0                   0                   1   \n",
       "1                   0                   0                   0   \n",
       "2                   0                   0                   0   \n",
       "3                   0                   0                   0   \n",
       "4                   0                   0                   0   \n",
       "\n",
       "   date_diff_bin_3650  date_diff_bin_99999  \n",
       "0                   0                    0  \n",
       "1                   0                    0  \n",
       "2                   0                    0  \n",
       "3                   0                    0  \n",
       "4                   0                    0  \n",
       "\n",
       "[5 rows x 42 columns]"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fe_member_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 保存结果到文件\n",
    "fe_member_data.to_csv(dpath + 'LR_data/FE_Members.csv', index=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
